package com.szty.lib_pen

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.lifecycle.Lifecycle
import com.sjh.baseutils.loading.LoadingType
import com.sjh.lib_app.HintMessageUtils
import com.sjh.lib_app.bean.*
import com.sjh.lib_app.net.NetConfig
import com.sjh.net.Method
import com.sjh.net.RequestType
import com.sjh.net.request

/**
 *    author : SunJH
 *    date   : 2021/5/27 0027
 *    desc   : 这是智能笔模块的公共网络上传模块
 */

/**
 * 上传分数的公共网络请求
 * @param mLifecycle 页面生命周期
 * @param exam_id 考试id
 * @param nowCheckStudent 当前需要上传的学生
 * @param result 请求结果回调
 */
fun postStundentScore(
    mLifecycle: Lifecycle,
    exam_id: String,
    nowCheckStudent: CheckStudent,
    result: (Request<Any>) -> Unit
) {
    val upLoadScore = mutableMapOf<String, String>()
    if (nowCheckStudent.answer == null || nowCheckStudent.answer!!.list.isEmpty()) {
        HintMessageUtils.showHintMessage(false, "未获取到学生考试数据")
        return
    }
    nowCheckStudent.answer!!.list.forEachIndexed { index, questionData ->
        if (questionData.getIsRight() != 99) {
            val is_right = if (questionData.getIsRight() == -1) {
                if (questionData.auto_correct_info == null) {
                    "0"
                } else {
                    questionData.auto_correct_info!!.is_right.toString()
                }
            } else {
                questionData.is_right
            }
            upLoadScore["correct_info[$index][result_score]"] =
                questionData.result_score
            upLoadScore["correct_info[$index][is_right]"] = is_right
            upLoadScore["correct_info[$index][question_number]"] =
                questionData.question_number
            upLoadScore["correct_info[$index][question_id]"] = questionData.question_id
            upLoadScore["correct_info[$index][owner_id]"] = questionData.owner_id.toString()
            upLoadScore["correct_info[$index][section_id]"] = questionData.section_id.toString()
            upLoadScore["correct_info[$index][book_id]"] = questionData.book_id.toString()
            upLoadScore["correct_info[$index][page_id]"] = questionData.page_id.toString()
        }
    }
    val params1 = StringBuffer()
    upLoadScore.forEach {
        params1.append(it.key)
        params1.append("=")
        params1.append(it.value)
    }
    request<Request<Any>>(preBlock = { NetConfig.appConfig(this) }, options = {
        method = Method.POST
        lifecycle = mLifecycle
    }) {
        url = "/api/app/exam/online-correct-save"
        loading = LoadingType.GENERAL_AND_NO_CANCEL
        requestType = RequestType.NO_PROCESSING
        params = {
            "student_id" to nowCheckStudent.student_id.toString()
            "exam_id" to exam_id
            this.map(upLoadScore)
        }
        onError = {
            it.printStackTrace()
            result.invoke(Request(0, "上传分数失败", "201", null))
        }
        onResult = { s: String, request: Request<Any>, b: Boolean, i: Int ->
            result.invoke(request)
        }
    }
}

fun nowPostStundentScore(
    mLifecycle: Lifecycle,
    exam_id: String,
    student_id: String,
    loadingType: LoadingType = LoadingType.GENERAL,
    questionData: QuestionData,
    result: (Request<UpLoadScore>) -> Unit
) {
    request<Request<UpLoadScore>>(preBlock = { NetConfig.appConfig(this) }, options = {
        method = Method.POST
        lifecycle = mLifecycle
    }) {
        url = "/api/app/exam/online-correct-save-single"
        loading = loadingType
        requestType = RequestType.NO_PROCESSING
        params = {
            "student_id" to student_id
            "exam_id" to exam_id
            "question_number" to questionData.question_number
            "question_id" to questionData.question_id
            "is_right" to questionData.is_right
            "result_score" to questionData.result_score
            "page_id" to questionData.page_id.toString()
            "book_id" to questionData.book_id.toString()
            "section_id" to questionData.section_id.toString()
            "owner_id" to questionData.owner_id.toString()
        }
        onError = {
            it.printStackTrace()
            result.invoke(Request(0, "上传分数失败", "201", null))
        }
        onResult = { s: String, request: Request<UpLoadScore>, b: Boolean, i: Int ->
            result.invoke(request)
        }
    }


}